package com.xoa.controller.crm;


import com.xoa.model.crm.Supplier;
import com.xoa.model.enclosure.Attachment;
import com.xoa.service.crm.SupplierService;
import com.xoa.service.sms.SmsService;
import com.xoa.util.ExcelUtils;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @Resource
    private SmsService smsService;

    //显示页面
    @RequestMapping("/showSupplier")
    public String myAttendance(HttpServletRequest request) {
        return "/app/crm/supplier/showSupplier";
    }

    //添加页面
    @RequestMapping("/saveSupplier")
    public String newLandResources() {
        return "app/crm/supplier/saveSupplier";
    }

    //修改页面
    @RequestMapping("/updateSupplier")
    public String updateLandResources() {
        return "app/crm/supplier/updateSupplier";
    }

    //导入页面
    @RequestMapping("/supplierImport")
    public String supplierImport(){
        return "app/crm/supplier/supplierImport";
    }

    //详情页面
    @RequestMapping("/detailSupplier")
    public String detailSupplier(){
        return "app/crm/supplier/detailSupplier";
    }

    //资质证书  certification
    @RequestMapping("/certification")
    public String certification(){
        return "app/crm/certification/certification";
    }

    //项目开标  bidOpening
    @RequestMapping("/bidOpening")
    public String bidOpening(){
        return "app/crm/certification/bidOpening";
    }

    //资质证书 新建 certification
    @RequestMapping("/newAdd")
    public String newAdd(HttpServletRequest request){
        String bodyId = request.getParameter("bodyId");
        if (!StringUtils.checkNull(bodyId)){
            smsService.setSmsReadStatus(request,Integer.parseInt(bodyId));
        }
        return "app/crm/certification/newAdd";
    }

    /**
     * 模糊查询  分页
     * @param request
     * @param page
     * @param pageSize
     * @param useFlag
     * @param supplier
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectByCount", produces = {"application/json;charset=UTF-8"})
    public ToJson<Supplier> selectByCount(HttpServletRequest request,HttpServletResponse response, Integer page, Integer pageSize, Boolean useFlag, Supplier supplier){
        Map<String,Object> map= new HashMap<String, Object>();
        supplier.setIsDeleted(Byte.valueOf("0"));
        map.put("obj",supplier);
        ToJson json =new ToJson<Supplier>(1,"err");

        try {
            List<Supplier> data =supplierService.selectByCondition(page, pageSize, useFlag, map);
            if(data!=null){
                PageParams pageParams = (PageParams) map.get("page");
                json.setTotleNum(pageParams.getTotal());
                json.setObj(data);
                json.setFlag(0);
                json.setMsg("true");
            }

        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     *保存一条供应商数据
     * @param request
     * @param supplier
     * @return
     */
    @RequestMapping("/insertSupplier")
    @ResponseBody
    public ToJson<Integer> insertSupplier(HttpServletRequest request, Supplier supplier){
        ToJson json =new ToJson<Integer>(1,"err");
        try {
            Integer data =supplierService.insert(supplier);
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 数据回显
     * @param request
     * @param supplierId
     * @return
     */
    @RequestMapping("/selectByPrimaryKey")
    @ResponseBody
    public ToJson<Supplier> selectByPrimaryKey(HttpServletRequest request, Integer supplierId){
        ToJson json =new ToJson<Supplier>(1,"err");
        try {
            Supplier data =supplierService.selectByPrimaryKey(supplierId);
            //添加附件处理
            List<Attachment> attachmentList=new ArrayList<Attachment>();
            if(data.getAttachmentId()!=null&&!"".equals(data.getAttachmentId())){
                attachmentList= GetAttachmentListUtil.returnAttachment(data.getAttachmentName(),data.getAttachmentId(),"xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_attachment);
            }
            data.setAttachmentList(attachmentList);
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 编辑供应商数据
     * @param request
     * @param supplier
     * @return
     */
    @RequestMapping("/updateByPrimarySupplier")
    @ResponseBody
    public ToJson<Integer> updateByPrimarySupplier(HttpServletRequest request, Supplier supplier){
        ToJson json =new ToJson<Integer>(1,"err");
        try {
            Integer data =supplierService.updateByPrimaryKey(supplier);
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 删除一条数据
     * @param request
     * @param supplierId
     * @return
     */
    @RequestMapping("/deleteByPrimaryKey")
    @ResponseBody
    public ToJson<Integer> deleteByPrimaryKey(HttpServletRequest request, Integer supplierId){
        ToJson json =new ToJson<Integer>(1,"err");
        try {
            Integer data =supplierService.deleteByPrimaryKey(supplierId);
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 导出功能
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/supplierExport", produces = {"application/json;charset=UTF-8"})
    public ToJson<Supplier> queryExportUsers(HttpServletRequest request, HttpServletResponse response) {
        return supplierService.supplierExport(request,response);
    }


    /**
     * 导入功能
     * @param request
     * @param response
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertImport", produces = {"application/json;charset=UTF-8"})
    public ToJson<Supplier> insertImport(HttpServletRequest request, HttpServletResponse response, MultipartFile file) {
        return supplierService.insertImport(request,response,file);
    }

}
